package com.dmwork.service.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.service.work.entity.Work;
import com.dmwork.service.work.entity.WorkPictureInfo;
import com.dmwork.service.work.mapper.WorkMapper;
import com.dmwork.service.work.mapper.WorkPictureInfoMapper;
import com.dmwork.service.work.service.WorkPictureInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmwork.service.work.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 李天
 * @since 2021-03-14
 */
@Service
public class WorkPictureInfoServiceImpl extends ServiceImpl<WorkPictureInfoMapper, WorkPictureInfo> implements WorkPictureInfoService {

    @Autowired
    private WorkMapper workMapper;

    @Override
    public List<String> getPictureUrlListByWorkId(String workId) {
        QueryWrapper<WorkPictureInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id", workId);
        List<WorkPictureInfo> workPictureInfoList = baseMapper.selectList(queryWrapper);
        List<String> pictureUrlList = new ArrayList<>();
        for (WorkPictureInfo workPictureInfo : workPictureInfoList) {
            pictureUrlList.add(workPictureInfo.getPictureUrl());
        }
        return pictureUrlList;
    }

    @Override
    public void removeByWorkId(String workId) {
        QueryWrapper<WorkPictureInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id", workId);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public void removePictureInfoByIds(String workId, List<String> idList) {
        //更新记录中的图片数量
        Work work = workMapper.selectById(workId);
        work.setPictureCount(work.getPictureCount() - idList.size());
        workMapper.updateById(work);
        //删除图片信息
        baseMapper.deleteBatchIds(idList);
    }
}
